package com.dianrui.fmi.fragment;

import android.os.Handler;
import android.widget.ListView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.adapter.InformtationAdapter;
import com.dianrui.fmi.bean.Informtaion;
import com.dianrui.fmi.event.RfreshInfomation;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.Url;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ImportantInformationFragment extends BaseFragment {
    //重要资讯
    private List<Informtaion> informtaionsList = new ArrayList<>();
    private InformtationAdapter informtationAdapter;

    @BindView(R.id.listview)
    ListView listview;

    @Override
    public int getContentViewId() {
        return R.layout.frag_importinformtion;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        informtationAdapter=new InformtationAdapter(getActivity(),informtaionsList);
        listview.setAdapter(informtationAdapter);
        getInformationData();
    }

    @Override
    public void getData() {}

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RfreshInfomation rfreshInfomation) {
        getInformationData();
    }

    private void getInformationData() {
        OkHttpUtils.getInstance().postWithMemberToken(Url.INDEX, "",  new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && isAdded()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (informtaionsList != null) {
                                    informtaionsList.clear();
                                }
                                JSONArray news_list = jsonObject.optJSONObject("data").optJSONArray("news_list");
                                if (news_list.length() > 0) {
                                    for (int i = 0; i < news_list.length(); i++) {
                                        Informtaion informtaion = new Informtaion();
                                        informtaion.news_id = news_list.optJSONObject(i).optString("news_id");
                                        informtaion.title = news_list.optJSONObject(i).optString("title");
                                        informtaion.introduction = news_list.optJSONObject(i).optString("introduction");
                                        informtaion.picture = news_list.optJSONObject(i).optString("picture");
                                        informtaion.create_time = news_list.optJSONObject(i).optString("create_time");
                                        informtaion.jump_url = news_list.optJSONObject(i).optString("jump_url");
                                        informtaionsList.add(informtaion);
                                    }
                                    if (informtationAdapter != null) {
                                        informtationAdapter.refresh(informtaionsList);
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
